package com.mall.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JQGridUtil {
	
	
	/**
	 * 处理排序方式，如果传入参数不为 "asc" 或 "desc" 则返回默认值 "asc"
	 * **/
	public static String wrapSortOrder(String sortOrder)
	{
		if(sortOrder == null ||((!sortOrder.equals("asc")) && (!sortOrder.equals("desc"))))
		{
			sortOrder = "asc";
		}
			
		return sortOrder;
	}
	
	/**
	 * 组织排序语句
	 * 如order by id desc
	 * **/
	public static String wrapSortString(String sortIndex,String sortOrder)
	{
		StringBuffer buffer = new StringBuffer();
		if(sortIndex != null && sortIndex.trim().length() > 0)
		{
			buffer.append(" order by ");
			buffer.append(sortIndex);
			buffer.append(" ");
			buffer.append(wrapSortOrder(sortOrder));
		}
		
		return buffer.toString();
	}
	
	/**
	 * 生成分页语句
	 * rn > 0 and rn <20
	 * 如果 rows <0 ，则不生成分页语句
	 */
	public static String wrapPageString(Integer page,Integer rows)
	{
		StringBuffer buffer = new StringBuffer();
		if(page!=null && rows!=null && rows>=0)
		{
			int startIndex = (page-1) * rows;
			int endIndex = page*rows;
			buffer.append(" rn >"+startIndex+" and rn <="+endIndex+" ");
		}
		return buffer.toString();
	}
	
	
	/**
	 * 组织过滤语句
	 * 如 and a = 'b'
	 * **/
	public static String warpFilters(String filters){
		String criterion = "";
		if(filters!=null && !filters.equals("")){			
			JSONObject fjson = JSONObject.fromObject(filters);				//解析filters,把fileters转换为json对象
			JSONArray jarray = fjson.getJSONArray("rules");					//获得rules数组
			String groupOp   = (String) fjson.get("groupOp");				//匹配规则and、or
			int i = 0;			
			for(Object o:jarray){
				JSONObject jobject = (JSONObject) o;
				String field = jobject.getString("field");					//列名
				String op    = jobject.getString("op");						//操作符
				String data  = jobject.getString("data").trim();			//数据
				int type = 0;
				if(!data.equals("")){
					if(field.endsWith("date") || field.endsWith("DATE")){
						data = "to_date('"+data+"','yyyy-mm-dd')";			//日期查询时，进行转换
						type = 1;
					}
					criterion = criterion +field+JavaUtil.toOperator(op, data, type);	//拼接查询条件
					if(i<jarray.size()-1){
						criterion+=" "+groupOp+" ";
						i++;
					}	
				}else {
					criterion = "";
				}	
							
			}
			if(!criterion.equals("")){
				if(groupOp.equals("or")){
					criterion = "("+criterion+")";
				}
				criterion +=" and 1=1";
			}			
		}
		return criterion;
	}
	
	/**
	 * 转换查询操作符
	 * @param op,data
	 * @return String
	 */
	public static String toOperator(String op,String data,int type){
		String Operator = "";
		if(op!=null && !op.equals("") && data!=null && !data.equals("")){
			if(type==0){
				if(op.equals("eq")){
					Operator = "="+"'"+data+"'";
				}else if(op.equals("ne")){
					Operator = "<>"+"'"+data+"'";
				}else if(op.equals("lt")){
					Operator = "<"+"'"+data+"'";
				}else if(op.equals("le")){
					Operator = "<="+"'"+data+"'";
				}else if(op.equals("gt")){
					Operator = ">"+"'"+data+"'";
				}else if(op.equals("ge")){
					Operator = ">="+"'"+data+"'";
				}else if(op.equals("bw")){
					Operator = " like "+"'"+data+"%'";
				}else if(op.equals("bn")){
					Operator = " not like "+"'"+data+"%'";
				}else if(op.equals("in")){
					Operator = "="+"'"+data+"'";
				}else if(op.equals("ni")){
					Operator = "<>"+"'"+data+"'";
				}else if(op.equals("ew")){
					Operator = " like "+"'%"+data+"'";
				}else if(op.equals("en")){
					Operator = " not like "+"'%"+data+"'";
				}else if(op.equals("cn")){
					Operator = " like "+"'%"+data+"%'";
				}else if(op.equals("nc")){
					Operator = " not like "+"'%"+data+"%'";
				}
			}else if(type == 1){
				if(op.equals("eq")){
					Operator = "="+data;
				}else if(op.equals("ne")){
					Operator = "<>"+data;
				}else if(op.equals("lt")){
					Operator = "<"+data;
				}else if(op.equals("le")){
					Operator = "<="+data;
				}else if(op.equals("gt")){
					Operator = ">"+data;
				}else if(op.equals("ge")){
					Operator = ">="+data;	
				}
			}
			
		}
		return Operator;
	}
	
	/**
	 * @param totalCount 总记录数
	 * 	@param perPageCount 每页记录数
	 * @return 返回总页数
	 * **/
	public static  int pageCount(int totalCount,int perPageCount)
	{
		int totalPage = 0;
		if((totalCount%perPageCount) == 0)
		{
			totalPage = totalCount /perPageCount;
		}
		else 
		{
			totalPage = totalCount / perPageCount + 1;
		}
		
		return totalPage;
	}
}
